package com.zyl.pts.service.serviceImpl;

import com.zyl.pts.config.enums.ResultCode;
import com.zyl.pts.config.exception.ServiceException;
import com.zyl.pts.domain.bean.Hurdles;
import com.zyl.pts.mapper.HurdlesMapper;
import com.zyl.pts.service.HurdlesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @version v1.0
 * @ProjectName: product_source
 * @Package: com.zyl.pts.service.serviceImpl
 * @ClassName: HurdlesServiceImpl
 * @Description TODO()
 * @Author 翟玉龙
 * @Date 2024/11/21-11:49
 */
@Service
public class HurdlesServiceImpl implements HurdlesService {

    private final HurdlesMapper hurdlesMapper;

    public HurdlesServiceImpl(HurdlesMapper hurdlesMapper) {
        this.hurdlesMapper = hurdlesMapper;
    }

    /**
     * 修改栏舍的动物存储数量
     * @param hurdlerId 栏舍id
     * @param changeNum 修改数量
     */
    @Transactional
    @Override
    public void changeSaved(String hurdlerId, int changeNum) {
        // 判断栏舍是否存在
        Hurdles hurdles = hurdlesMapper.queryById(hurdlerId);
        if(hurdles == null) {
            throw new ServiceException(ResultCode.HURDLES_IS_NOT_EXIST);
        }
        int num = hurdles.getHSaved() + changeNum;
        // 判断栏舍是否已满
        if(num > hurdles.getHMax()) {
            throw new ServiceException(ResultCode.HURDLES_IS_FULL);
        }
        if(num < 0) {
            throw new ServiceException(ResultCode.HURDLES_IS_EMPTY);
        }
        String full = num == hurdles.getHMax() ? "已满" : "未满";

        hurdlesMapper.changeSaved(hurdlerId, num, full);
    }
}
